﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Medianamik.Core;
using Medianamik.Core.Interfaces;
using Medianamik.Core.Synchronization;
using Medianamik.Globalization;
using Medianamik.UI.Lock;
using Telerik.Web.UI;
using Medianamik.Core.Configuration;
using System.Configuration;
using System.Globalization;
using Medianamik.Core.Profile;
using Medianamik.Core.Sugar;

namespace Medianamik.UI.Controls
{
    public class MedianamikLookup : WebControl, INamingContainer
    {
        public string TabName
        {
            get
            {
                return ViewState["TabName2"] == null ? null :
                    ViewState["TabName2"].ToString();
            }
            set { ViewState["TabName2"] = value; }
        }

        public string TypeName
        {
            get
            {
                return ViewState["TypeName"] == null ? null :
                    ViewState["TypeName"].ToString();
            }
            set { ViewState["TypeName"] = value; }
        }

        public virtual string LookupPropertyName
        {
            get
            {
                return ViewState["LookupPropertyName"] == null ? null :
                    ViewState["LookupPropertyName"].ToString();
            }
            set { ViewState["LookupPropertyName"] = value; }
        }

        [Browsable(false)]
        public Record[] DataSource
        {
            get
            {
                return (ViewState["DataSource2"] == null ? new Record[] { } :
                    ViewState["DataSource2"] as Record[]);
            }
            set { ViewState["DataSource2"] = value; }
        }

        public string HiddenProperties
        {
            get
            {
                return ViewState["HiddenProperties2"] == null ? string.Empty :
                    ViewState["HiddenProperties2"].ToString();
            }
            set { ViewState["HiddenProperties2"] = value; }
        }

        [Obsolete("Use the AllowLinesReordering property")]
        public bool AllowSorting
        {
            get
            {
                return AllowLinesReordering;
            }
            set { AllowLinesReordering = value; }
        }

        public bool AllowLinesReordering
        {
            get
            {
                return ViewState["AllowLinesReordering"] == null ? false :
                   (bool)ViewState["AllowLinesReordering"];
            }
            set { ViewState["AllowLinesReordering"] = value; }
        }

        public bool AllowOrderByColumn
        {
            get
            {
                return ViewState["AllowOrderByColumn"] == null ? false :
                   (bool)ViewState["AllowOrderByColumn"];
            }
            set { ViewState["AllowOrderByColumn"] = value; }
        }

        public MedianamikLookupColumn[] Columns
        {
            get
            {
                return ViewState["MedianamikLookupColumns"] == null ? new MedianamikLookupColumn[] { } :
                    ViewState["MedianamikLookupColumns"] as MedianamikLookupColumn[];
            }
            set { ViewState["MedianamikLookupColumns"] = value; }
        }

        public string AddButtonText
        {
            get
            {
                return ViewState["AddButtonText"] == null ? string.Empty :
                    ViewState["AddButtonText"].ToString();
            }
            set { ViewState["AddButtonText"] = value; }
        }

        public IEnumerable<Record> GetRecordsForGrid(string customSort, bool ascending)
        {
            IEnumerable<Record> returnedData;
            var dataSource = DataSource
                .Where(r => r.SynchronizeAction != SynchronizeActions.DeleteBranch &&
                r.SynchronizeAction != SynchronizeActions.Delete);

            if (AllowOrderByColumn && !String.IsNullOrEmpty(customSort))
            {
                if (ascending)
                {
                    returnedData = dataSource.OrderBy(r => r.Properties.First(p => String.Compare(p.NodeTypePropertyName, customSort, true) == 0
                        || p.NodeTypePropertyId.Equals(MedianamikProperties.Name)).Value.ToString());
                }
                else
                {
                    returnedData = dataSource.OrderByDescending(r => r.Properties.First(p => String.Compare(p.NodeTypePropertyName, customSort, true) == 0
                        || p.NodeTypePropertyId.Equals(MedianamikProperties.Name)).Value.ToString());
                }
            }
            else if (AllowLinesReordering)
            {
                returnedData = dataSource.OrderBy(r => r.Position);
            }
            else
            {
                returnedData = dataSource.OrderBy(r => r.Properties.First(p => p.NodeTypePropertyName.ToLower().Equals("name")
                     || p.NodeTypePropertyId.Equals(MedianamikProperties.Name)).Value.ToString());
            }
            return returnedData;
            //if (AllowSorting)
            //{
            //    //return DataSource
            //    //    .Where(r => r.SynchronizeAction != SynchronizeActions.DeleteBranch &&
            //    //        r.SynchronizeAction != SynchronizeActions.Delete)
            //    //    .OrderBy(r => r.Position);
            //    var dataSource = DataSource
            //        .Where(r => r.SynchronizeAction != SynchronizeActions.DeleteBranch &&
            //            r.SynchronizeAction != SynchronizeActions.Delete);
            //    //return dataSource.OrderBy(r => r.Position);
            //    IOrderedEnumerable<Record> orderedDatasource;
            //    if (String.IsNullOrEmpty(customSort))
            //    {
            //        orderedDatasource = dataSource.OrderBy(r => r.Position);
            //    }
            //    else
            //    {
            //        if (ascending)
            //        {
            //            orderedDatasource = dataSource.OrderBy(r => r.Properties.First(p => String.Compare(p.NodeTypePropertyName, customSort, true) == 0
            //                || p.NodeTypePropertyId.Equals(MedianamikProperties.Name)).Value.ToString());
            //        }
            //        else
            //        {
            //            orderedDatasource = dataSource.OrderByDescending(r => r.Properties.First(p => String.Compare(p.NodeTypePropertyName, customSort, true) == 0
            //                || p.NodeTypePropertyId.Equals(MedianamikProperties.Name)).Value.ToString());
            //        }

            //    }
            //    return orderedDatasource;
            //}

            //return DataSource.Where(r => r.SynchronizeAction != SynchronizeActions.DeleteBranch &&
            //     r.SynchronizeAction != SynchronizeActions.Delete)
            //     .OrderBy(r => r.Properties.First(p => p.NodeTypePropertyName.ToLower().Equals("name")
            //         || p.NodeTypePropertyId.Equals(MedianamikProperties.Name)).Value.ToString());
        }

        public IEnumerable<Record> GetRecordsForGrid()
        {
            return GetRecordsForGrid(String.Empty, true);
        }
    }
}